package p3;
/**
 * This class implements functionality associated
 * with the I/O device of the simulated system.
 * 
 */
public class InputOutputDevice implements DeviceInterface {
	
	/** This queue holds the processes that has requested I/O*/ 
	private Queue inputOutputQueue;
	
	/** Reference to the statistics collector */
	private Statistics statistics;
	
	/**
	 * Creates a new I/O device
	 * 
	 * @param inputOutputQueue	The I/O queue to be used 
	 * @param avgExecutionTime	The average time of an I/O operation
	 * @param stat				Reference to the statistics object
	 */
	
	public InputOutputDevice(Queue inputOutputQueue, Statistics stat) {
		this.inputOutputQueue = inputOutputQueue;
		this.statistics = stat;
	}

	/**
	 * Makes a process 'p' request I/O time
	 */
	public void insertProcess(Process p) {
		this.inputOutputQueue.insert(p);
	}

	@Override
	public void timePassed(long timePassed) {
		statistics.ioQueueLengthTime += inputOutputQueue.getQueueLength()*timePassed;
		if(inputOutputQueue.getQueueLength() > statistics.ioQueueLargestLength) {
			statistics.ioQueueLargestLength = inputOutputQueue.getQueueLength();
		}		
	}

	@Override
	public Process popNextProcess() {
		Process p = (Process)inputOutputQueue.getNext();
		inputOutputQueue.removeNext();
		return p;
	}
	
	public Process getNextProcess() {
		return (Process)inputOutputQueue.getNext();
	}
	
	public long getQueueLength() {
		return inputOutputQueue.getQueueLength();
	}
	
	public boolean isEmpty() {
		return inputOutputQueue.getQueueLength() == 0 ? true : false;
	}
}
